package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyPersistenceStatisVO;
import cn.wolfcode.luowowo.cache.service.IRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务:定时通过redis中的数据
 */
@Component
public class RedisDataPersistenceJob {

    @Reference
    private IRedisService redisService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;
    //指定doword方法运行的规则
    //定时任务执行计划
    //秒 分 小时 月份中的日期 月份 星期中的日期 年份
    //0   0  0     1/1     *      ?
    //这个注解专门用于做定时器的
    @Scheduled(cron = "59 59 23 * * ?")
    public void dowork() {
        System.out.println("redis同步数据库开启");

        List<StrategyStatisVO> vos  = strategyStatisRedisService.getStatisVos();
        for (StrategyStatisVO vo : vos) {

            StrategyPersistenceStatisVO v = new StrategyPersistenceStatisVO();
            v.setThumbsupnum(vo.getThumbsupnum());
            v.setFavornum(vo.getFavornum());
            v.setReplynum(vo.getReplynum());
            v.setStrategyId(vo.getStrategyId());
            v.setViewnum(vo.getViewnum());

            strategyDetailService.updateByRedis(v);
        }
    }
}
